package Demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-04-29
 * Time: 20:10
 */
public class MyLinkedList {
    static class ListNode {

        public int val;
        public ListNode prev;// 前驱
        public ListNode next;// 后驱

        public ListNode(int val) {
            this.val = val;
        }

    }

    public ListNode head;   //头节点
    public ListNode tail;   //尾节点

    // 打印双链表
    public void display() {
        if (head == null) {
            throw new RuntimeException("链表为空, 无法打印!");
        }
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    // 求解双链表长度(节点个数)
    public int size() {
        if (head == null) {
            throw new RuntimeException("链表为空!");
        }
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    // 头插法
    public void addFist(int data) {
        ListNode node = new ListNode(data);
        // 一个节点没有, 首次插入
        if (head == null) {
            head = node;
            tail = node;
            return;
        }
        // 非首次插入
        // 绑定后面
        node.next = head;
        head.prev = node;
        head = node;
    }

    // 尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            tail = node;
            return;
        }
        tail.next = node;
        node.prev = tail;
        // tail = node;
        tail = tail.next;
    }

    // 查找是否包含某个 val 值的节点
    public boolean contains(int key) {
        if (head == null) {
            throw new RuntimeException("当前链表为空, 无法查询!");
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 从0下标开始, 在任意位置插入节点
    public void addIndex(int data, int index) {
        if (index < 0 || index > size()) {
            throw new RuntimeException("index 位置不合法!");
        }
        if (index == 0) {
            // 头插
            addFist(data);
            return;
        }
        if (index == size()) {
            // 尾插
            addLast(data);
            return;
        }
        ListNode cur = head;
        ListNode node = new ListNode(data);
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        // 修改指向
        node.next = cur.next;
        cur.next.prev = node;
        node.prev = cur;
        cur.next = node;
    }

    // 删除第一次出现的指定节点
    public void delete(int key) {
        if (head == null) {
            throw new RuntimeException("链表为空, 无法删除!");
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                // 要删除的节点
                if (cur == head) {
                    // 要删除的为头结点
                    head = head.next; // 头结点指向下一个节点
                    if (head != null) {
                        // 有多个节点, 但删除的是第一个节点
                        head.prev = null; // 下一个节点作为头接的 prev 为null
                    } else {
                        // 只有一个节点, 并且当前节点为要删除的节点时,
                        tail = null;
                    }
                } else {
                    // 删除的不是头节点
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        tail = cur.prev;
                    }
                }
                return;
            } else {
                cur = cur.next;
            }
        }
    }

    // 删除所有值为 key 的指定节点
    public void deleteAll(int key) {
        if (head == null) {
            throw new RuntimeException("链表为空, 无法删除!");
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                // 要删除的节点
                if (cur == head) {
                    // 要删除的为头结点
                    head = head.next; // 头结点指向下一个节点
                    if (head != null) {
                        // 有多个节点, 但删除的是第一个节点
                        head.prev = null; // 下一个节点作为头接的 prev 为null
                    } else {
                        // 只有一个节点, 并且当前节点为要删除的节点时,
                        tail = null;
                    }
                } else {
                    // 删除的不是头节点
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        tail = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    // 清空节点, 回收内存-清空引用
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = head.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }
}
